﻿using FriendStar.Code;
using FriendStar.Domain.Entity.BaseData;
using FriendStar.Domain.IRepository.BaseData;
using FriendStar.Repository.BaseData;
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using System;
using System.Data.SqlClient;
using FriendStar.Application.VBaseData;
using System.Data;

namespace FriendStar.Application.BaseData
{
    public class ZSJ_B1_GH_SIApp
    {
        //制造单位
        private IZSJ_B1_GH_SIRepository service = new ZSJ_B1_GH_SIRepository();
        public List<ZSJ_B1_GH_SIEntity> GetList(string keyword, string isProduct)
        {
            var expression = ExtLinq.True<ZSJ_B1_GH_SIEntity>();
           
            if (!string.IsNullOrEmpty(isProduct))
            {
                expression = expression.And(t => t.IsProduct == isProduct);
            }
            return service.IQueryable(expression).OrderBy(t => t.UnitNO).ToList();
        }
        public List<ZSJ_B1_GH_SIEntity> GetProCompanyName(string useUnit, string isProduct)
        {
            var expression = ExtLinq.True<ZSJ_B1_GH_SIEntity>();
            if (!string.IsNullOrEmpty(useUnit))
            {
                expression = expression.And(t => t.UseUnit.Contains(useUnit));
            }
            if (!string.IsNullOrEmpty(isProduct))
            {
                expression = expression.And(t => t.IsProduct == isProduct);
            }
            return service.IQueryable(expression).Select(p => new ZSJ_B1_GH_SIEntity() {UnitNO=p.UnitNO,UnitName=p.UnitName}).OrderBy(t => t.UnitNO).ToList();
        }

        public List<ZSJ_B1_GH_SIEntity> GetPickCompany(string useUnit, string isCustomer)
        {
            var expression = ExtLinq.True<ZSJ_B1_GH_SIEntity>();
            if (!string.IsNullOrEmpty(useUnit))
            {
                expression = expression.And(t => t.UseUnit.Contains(useUnit));
            }
            if (!string.IsNullOrEmpty(isCustomer))
            {
                expression = expression.And(t => t.IsCustomer == isCustomer);
            }
            return service.IQueryable(expression).ToList();
        }
        /// <summary>
        /// 冲压车间
        /// </summary>
        /// <param name="useUnit"></param>
        /// <param name="isStampingRoom"></param>
        /// <returns></returns>
        public List<ZSJ_B1_GH_SIEntity> GetStampingOnline(string useUnit, string isStampingRoom)
        {
            var expression = ExtLinq.True<ZSJ_B1_GH_SIEntity>();
            if (!string.IsNullOrEmpty(useUnit))
            {
                expression = expression.And(t => t.UseUnit.Contains(useUnit));
            }
            if (!string.IsNullOrEmpty(isStampingRoom))
            {
                expression = expression.And(t => t.IsCYCJ == isStampingRoom);
            }
            return service.IQueryable(expression).ToList();
        }

        public object GetProduceUnit(string unitcode)
        {
            List<Dictionary<string, object>> result = null;

            Dictionary<string, object> item = null;

            string sql = @"SELECT MakeLineNO, MakeLineShortName,MakeLineNO+' '+MakeLineShortName MakeLineNOName from LXK_16_LX_MakeLine   where UseUnit =@unitcode";

            result = new List<Dictionary<string, object>>();

            SqlParameter par = new SqlParameter("@unitcode", unitcode);

            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.GetConnection(), CommandType.Text, sql, par))
            {
                while (reader.Read())
                {
                    item = new Dictionary<string, object>();

                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        item.Add(reader.GetName(i), reader[i]);
                    }

                    result.Add(item);
                }
            }

            return result;
        }

        public object GeStocktList(string unitcode)
        {
            List<Dictionary<string, object>> result = null;

            Dictionary<string, object> item = null;

            string sql = @"SELECT FStoreNO,FStoreName,FStoreNO+' '+FStoreName UnitNOShortName  from CPK_ZD_11_DatStoreHouse   where UseUnit =@unitcode  AND FStoreType ='01'  ORDER BY FStoreNO";

            result = new List<Dictionary<string, object>>();

            SqlParameter par = new SqlParameter("@unitcode", unitcode);

            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.GetConnection(), CommandType.Text, sql,par))
            {
                while (reader.Read())
                {
                    item = new Dictionary<string, object>();

                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        item.Add(reader.GetName(i), reader[i]);
                    }

                    result.Add(item);
                }
            }

            return result;
        }

        public List<ZSJ_B1_GH_SIEntity> GetUnitShortName(string keyword,string isProduct)
        {
            var expression = ExtLinq.True<ZSJ_B1_GH_SIEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                 expression = expression.And(t => t.IsProduct.Contains(keyword));
                //expression = expression.And(t => t.IsProduct.Contains(keyword));
                //expression = expression.And(t => t.IsProduct =="0");
      
            }
            if (!string.IsNullOrEmpty(isProduct))
            {
                expression = expression.And(t =>t.IsProduct==isProduct);
            }
            return service.IQueryable(expression).OrderBy(t => t.UseUnit).ToList();
        }

 
        public ZSJ_B1_GH_SIEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.DeleteForm(keyValue);
        }
        public void SubmitForm(ZSJ_B1_GH_SIEntity userEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                userEntity.Modify(keyValue);
            }
            else
            {
                userEntity.Create();
            }
            service.SubmitForm(userEntity,  keyValue);
        }
        public void UpdateForm(ZSJ_B1_GH_SIEntity userEntity)
        {
            service.Update(userEntity);
        }
      
    }
}